package com.banyuan.dao.impl;

import com.banyuan.dao.IAccountDao;
import com.banyuan.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.util.List;

public class AccountDaoImpl implements IAccountDao {
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public int insert(Account account) {
        int insert = jdbcTemplate.update("insert into account(name,money) values (?,?)", account.getName(), account.getMoney());
        return insert;
    }

    @Override
    public int update(Account account) {
        int update = jdbcTemplate.update("update account set name = ?,money = ? where id = ?", account.getName(), account.getMoney(), account.getId());
        return update;
    }

    @Override
    public int delete(Integer id) {
        int delete = jdbcTemplate.update("delete from account where id = ?", id);
        return delete;
    }

    @Override
    public List<Account> selectAll() {
        RowMapper<Account> rowMapper = new BeanPropertyRowMapper<>(Account.class);
        List<Account> accountList = jdbcTemplate.query("select * from account", rowMapper);
        return accountList;
    }

    @Override
    public Account selectById(Integer id) {
        RowMapper<Account> rowMapper = new BeanPropertyRowMapper<>(Account.class);
        Account account = jdbcTemplate.queryForObject("select * from account where id = ?", rowMapper, id);
        return account;
    }
}
